<template>
  <div>
    <div class="content-header">
      <div class="container">

      </div>
    </div>

    <div class="content">
      <div class="container">

        <div class="row">

          <div class="col-lg-8">
            <form>
              <div class="form-group">
                <label for="paramsTextarea">包含转义的字符串</label>
                <textarea class="form-control" id="paramsTextarea" rows="8" v-model="paramsText"></textarea>
              </div>


                <el-form-item>

                  <el-button type="primary" @click="handleInputBlur">删除转义</el-button>

                    <el-button type="danger" @click="unicodeToChinese(paramsText)">unicode转中文</el-button>

                    <el-button type="info" @click="chineseToUnicode(paramsText)">中文转unicode</el-button>
                </el-form-item>

              <div class="form-group">
                <label for="curlResTextarea">删除转义结果</label>
                <textarea class="form-control" id="curlResTextarea" rows="8" placeholder="{}"
                  v-model="curlVal"></textarea>
              </div>
            </form>
          </div>

        </div>


        <!-- /.row -->
      </div>

    </div>
  </div>
</template>

<script>
import { post } from '@/utils/api'

export default {
  name: "zhuanYiIndex",
  data() {
    return {
      // val: '',
      curlVal: '',
      paramsText: '',
      formParam: {
        postParams: ''
      }
    };
  },
  methods: {
    // 定义一个方法，获取百度页面的标题
    getBaiduPageTitile() {
      // 调用封装好的 get 方法
      // get('https://www.baidu.com').then(res => {
      //   // 获取成功后将返回的数据赋值给 val
      //   this.val = res.data;
      // });
    },
    unicodeToChinese(unicodeStr) {
      this.curlVal = unicodeStr.replace(/\\u[\dA-Fa-f]{4}/g, (match) =>
        String.fromCharCode(parseInt(match.replace(/\\u/g, ''), 16))
      );
    },
    chineseToUnicode(chineseStr) {
      this.curlVal = chineseStr.replace(/[\u4e00-\u9fa5]/g, (match) =>
        '\\u' + match.charCodeAt(0).toString(16)
      );
    },
    handleInputBlur() {
      //console.log('oo')
      if (this.paramsText) {
        this.formParam.postParams = encodeURIComponent(this.paramsText);
        this.genPostReq();
      }
    },

    async genPostReq() {
      var reqParam = { req_url: '', post_params: '' }
      reqParam.req_url = this.formParam.reqUrl;
      reqParam.post_params = this.formParam.postParams;
      var url = '/tool/del_zhuan_yi';

      try {
        // 发送 application/json 类型的 POST 请求
        var retData = await post(url, reqParam, 'form');
        this.curlVal = retData.data
        // 发送 application/x-www-form-urlencoded 类型的 POST 请求 (取消注释以使用)
        // this.result = await post(url, data, 'form');
      } catch (error) {
        console.error('请求失败:', error);
      }

    },
  }
};
</script>

<style scoped></style>